﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Linq;

namespace ThreeByte.ActiveDeck.Data.UserResponse
{
    public class MultipleChoiceAnswerData : INotifyPropertyChanged
    {
        private static readonly XNamespace ns = Service.ServiceConfiguration.NAMESPACE;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public MultipleChoiceAnswerData(int value, bool isCorrect = false) {
            Value = value;
            IsCorrect = isCorrect;
        }

        public int Value { get; private set; }
        public bool IsCorrect { get; private set; }

        private int _userResponses;
        public int UserResponses {
            get {
                return _userResponses;
            }
            set {
                _userResponses = value;
                NotifyPropertyChanged("UserResponses");
            }
        }

        private int _percent;
        public int Percent {
            get {
                return _percent;
            }
            set {
                _percent = value;
                NotifyPropertyChanged("Percent");
            }
        }

        public XElement ToXml() {
            XElement node = new XElement(ns + "Answer");
            node.Add(new XAttribute("Value", Value));
            node.Add(new XAttribute("Responses", UserResponses));
            node.Add(new XAttribute("Percent", Percent));
            return node;
        }

        public static MultipleChoiceAnswerData ParseXml(XElement node) {
            MultipleChoiceAnswerData answerData = new MultipleChoiceAnswerData(int.Parse(node.Attribute("Value").Value));
            answerData.UserResponses = int.Parse(node.Attribute("Responses").Value);
            answerData.Percent = int.Parse(node.Attribute("Percent").Value);
            return answerData;
        }
    }
}
